// components/media-view/index.js
const { globalData } = getApp();

Component({
  lifetimes: {
    attached: function () {
      this.video = wx.createVideoContext("myVideo");
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {
    src: {
      type: Array,
      value: [],
      observer: function (list) {
        const imageArr = [];
        const videoArr = [];

        list.forEach((item) => {
          if (item.annex.indexOf(".mp4") > -1) {
            videoArr.push(`${globalData.imageUrl}${item.annex}`);
          } else if (
            item.annex.indexOf(".png") > -1 ||
            item.annex.indexOf(".jpg") > -1
          ) {
            imageArr.push(`${globalData.imageUrl}${item.annex}`);
          }
        });

        this.setData({
          imageArr,
          videoArr,
        });
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    imageArr: [],
    videoArr: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handelPreview(event) {
      const currentUrl = event.currentTarget.dataset.src;

      const { imageArr } = this.data;

      wx.previewImage({
        current: currentUrl,
        urls: imageArr,
      });
    },
    handlePlayVideo(event) {
      const src = event.currentTarget.dataset.src;

      const data = {
        video_url: src,
        title: "视频播放",
      };

      const str = encodeURIComponent(JSON.stringify(data));

      wx.navigateTo({
        url: "/pages/video-play/video-play?video=" + str,
      });
    },
  },
});
